package com.kfm.base.io.o;

import java.io.*;

public class Demo05 {

    public static void main(String[] args) {
        // 序列化
//        try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("text/oos1.txt"))){
//            Person p = new Person("秦爽", 20);
//            oos.writeObject(p);
//        } catch (FileNotFoundException e) {
//            throw new RuntimeException(e);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }

        // 反序列化
        try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("text/oos1.txt"))){

            Object o = ois.readObject();
            System.out.println(o);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

class Person implements Serializable {

    private static final long serialVersionUID = 1L;

    private String name;

    private int age;

    private String gender;

    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
